import React from 'react';
import PropTypes from 'prop-types';
import {Button, Form, Row, Col} from 'antd';
import TextInput from 'Shared/components/TextInput';
import {FORM_OPTIONS} from '../constants';
import {injectIntl, language} from './intl';
import WrappedSelect from '../common/WrappedSelect';
import DateRangePicker from 'Shared/components/DateRangePicker';
import DealerSelectPanel from '../common/DealerSelect/SelectEdit';
import TagSelect from 'Shared/components/TagSelect';
import {salesConfirmationStatus} from '../Enum';
const FormItem = Form.Item;
const statusList = salesConfirmationStatus.toList(language);

class QueryPanel extends React.PureComponent {
    handleFilterChange = (value, name) => {
        this.props.onChange(name, value);
    }
    onSelectDealer = data => {
        this.props.onChange('dealerId', data.id);
        this.props.onQueryDealerChange({
            id: data.id,
            code: data.code,
            name: data.name,
        });
    }
    render() {
        const {formatMessage} = this.props.intl;
        const dealerInfo = this.props.condition.dealerId ? {
            id: this.props.queryDealer.id,
            code: this.props.queryDealer.code,
            name: this.props.queryDealer.name
        } : null;
        return (
            <div>
                <Form className="form-standard">
                    <Row>
                        {!this.props.isDealerUser &&
                            <Col {...FORM_OPTIONS.col}>
                                <FormItem
                                    label={this.props.intl.formatMessage({
                                        id: 'queryPanel.form.dealerId',
                                        defaultMessage: '总代'
                                    })}
                                    {...FORM_OPTIONS.item}>
                                    <DealerSelectPanel
                                        value={dealerInfo}
                                        onSelect={this.onSelectDealer} />
                                </FormItem>
                            </Col>
                        }
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem
                                label={this.props.intl.formatMessage({
                                    id: 'queryPanel.form.brandId',
                                    defaultMessage: '品牌'
                                })}
                                {...FORM_OPTIONS.item}>
                                <WrappedSelect
                                    allowClear
                                    name="brandId"
                                    value={this.props.condition.brandId}
                                    options={this.props.initEnum.brands}
                                    onChange={this.handleFilterChange} />
                            </FormItem>
                        </Col>
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem label={formatMessage({
                                id: 'queryPanel.form.code',
                                defaultMessage: '确认书号'
                            })} {...FORM_OPTIONS.item}>
                                <TextInput
                                    name="code"
                                    value={this.props.condition.code}
                                    onPressEnter={this.handleFilterPressEnter}
                                    onBlur={this.handleFilterChange} />
                            </FormItem>
                        </Col>
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem label={formatMessage({
                                id: 'queryPanel.form.batchNumber',
                                defaultMessage: '批次号'
                            })} {...FORM_OPTIONS.item}>
                                <TextInput
                                    name="batchNumber"
                                    value={this.props.condition.batchNumber}
                                    onPressEnter={this.handleFilterPressEnter}
                                    onBlur={this.handleFilterChange} />
                            </FormItem>
                        </Col>
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem label={formatMessage({
                                id: 'queryPanel.form.createTime',
                                defaultMessage: '创建时间'
                            })} {...FORM_OPTIONS.item}>
                                <DateRangePicker
                                    id="createTime"
                                    value={this.props.condition.createTime}
                                    onChange={this.handleFilterChange} />
                            </FormItem>
                        </Col>
                    </Row>
                    <Row>
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem label={formatMessage({
                                id: 'queryPanel.form.status',
                                defaultMessage: '状态'
                            })} {...FORM_OPTIONS.item}>
                                <TagSelect
                                    name="status"
                                    value={this.props.condition.status}
                                    options={statusList}
                                    onChange={this.handleFilterChange} />
                            </FormItem>
                        </Col>
                    </Row>
                    <Row>
                        <Col span={6}>
                            <Button key="query" type="primary"
                                onClick={this.props.querySearch} loading={this.props.loading}>
                                {formatMessage({
                                    id: 'queryPanel.btn.search',
                                    defaultMessage: '查询'
                                })}</Button>
                            <Button key="clear" onClick={this.props.onClickReset}>{formatMessage({
                                id: 'queryPanel.btn.reset',
                                defaultMessage: '重置'
                            })}</Button>
                        </Col>
                    </Row>
                </Form>
            </div>
        );
    }
}

QueryPanel.propTypes = {
    condition: PropTypes.object.isRequired,
    loading: PropTypes.bool.isRequired,
    querySearch: PropTypes.func.isRequired,
    onChange: PropTypes.func.isRequired,
    onClickReset: PropTypes.func.isRequired,
    onQueryDealerChange: PropTypes.func.isRequired,
    isDealerUser: PropTypes.bool,
    queryDealer: PropTypes.object,
};

import {connect} from 'react-redux';
import {
    saveQueryCondition,
    querySearch,
    resetQueryPanel,
    saveQueryDealer
} from './actions';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
import {createSelector} from 'reselect';
const getConditions = selectorFactory(['page', 'appState', 'queryCondition']);
const getisDealerUser = selectorFactory(['page', 'domainData', 'initData', 'isDealerUser']);
const getQueryDealer = selectorFactory(['page', 'appState', 'queryDealer']);

const getInitData = createSelector(
    state => state.getIn(['page', 'domainData', 'initData']),
    init => {
        const initData = init.toJS();
        const brands = initData.brands ? initData.brands.map(item => ({
            text: `${'('}${item.code}${')'}${item.name}`,
            value: item.id
        })) : [];
        return {
            brands
        };
    }
);

const mapStateToProps = state => ({
    condition: getConditions(state),
    loading: state.getIn((['page', 'domainData', 'list', 'isFetching'])),
    initEnum: getInitData(state),
    queryDealer: getQueryDealer(state),
    isDealerUser: getisDealerUser(state)
});

const mapDispatchToProps = dispatch => ({
    onChange: (name, value) => dispatch(saveQueryCondition(name, value)),
    onQueryDealerChange: data => dispatch(saveQueryDealer(data)),
    querySearch: () => dispatch(querySearch()),
    onClickReset: () => dispatch(resetQueryPanel()),
});

export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(QueryPanel));
